//
//  HQCustomDialogs.m
//  PersonalOC
//
//  Created by 韩琴 on 2018/9/3.
//  Copyright © 2018年 韩琴. All rights reserved.
//

#import "HQCustomDialogs.h"


@interface HQCustomDialogs()
@property (assign,nonatomic) BOOL present;///记录当前是展开还是消失


@end
@implementation HQCustomDialogs
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source {
    PopoverViewController *pc = [[PopoverViewController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
    [pc.coverButton setEnabled:self.isCoverEnlable];
    pc.presentFrame = self.presentFrame;
    [pc.coverButton setBackgroundColor:self.backGrougdColor];
    return pc;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.presentFrame = CGRectZero;
        self.present = true;
        self.backGrougdColor = [[UIColor alloc] initWithWhite:0.5 alpha:0.5];
        self.isCoverEnlable = true;
    }
    return self;
}
//该方法用于返回一个负责转场如何出现的对象
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    
    self.present = true;
    [[NSNotificationCenter defaultCenter] postNotificationName:HQCustomDialogsDidPresented object:self];
    return self;
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.present = false;
    [[NSNotificationCenter defaultCenter] postNotificationName:HQCustomDialogsDidDismiss object:self];
    return self;
}
//MARK: - UIPresentationController
//告诉系统展现和消失的时长
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.2;
}
 //告诉该系统是如何展现和消失的，如我实现这个代理方法系统就不会添加默认的动画，所有的动画需要自己实现
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (self.present) {
         //取出toView
        UIView *toView = nil;
        if ([transitionContext viewForKey:UITransitionContextToViewKey] == nil) {
            return;
        }
        toView = [transitionContext viewForKey:UITransitionContextToViewKey];
        [transitionContext.containerView addSubview:toView];
        switch (self.prsentDeriction) {
            case HQPresentDerictionLeft:
                //为toView设置动画
                toView.transform = CGAffineTransformScale(toView.transform, 0.0, 1.0);
                //设置锚点
                toView.layer.anchorPoint = CGPointMake(0.0, 0.5);
                break;
            case HQPresentDerictionRight:
                //为toView设置动画
                toView.transform = CGAffineTransformScale(toView.transform, 0.0, 1.0);
                //设置锚点
                toView.layer.anchorPoint = CGPointMake(1.0, 0.5);
                break;
            case HQPresentDerictionBottom:
                //为toView设置动画
                toView.transform = CGAffineTransformScale(toView.transform, 1.0, 0.0);
                //设置锚点
                toView.layer.anchorPoint = CGPointMake(0.5, 1.0);
                break;
            default:
                //为toView设置动画
                toView.transform = CGAffineTransformScale(toView.transform, 1.0, 0.0);
                //设置锚点
                toView.layer.anchorPoint = CGPointMake(0.5, 0.0);
                break;
        }
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            toView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            //自定义转场动画，在执行完动画执行结束后，一定要告诉程序动画执行完毕
            [transitionContext completeTransition:true];
        }];
    } else {
        UIView *froView = nil;
        if ([transitionContext viewForKey:UITransitionContextFromViewKey] == nil) {
            return;
        }
        froView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            switch (self.prsentDeriction) {
                case (HQPresentDerictionLeft || HQPresentDerictionRight):
                    froView.transform = CGAffineTransformScale(froView.transform, 0.0001, 1);
                    
                    break;
                    
                default:
                    froView.transform = CGAffineTransformScale(froView.transform, 1.0, 0.0001);
                    break;
            }
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:true];
        }];
    }
}
@end
